package umg.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDesktopPane;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;

import umg.ChatReaderThread;
import umg.Constants;
import umg.Messages;
import umg.PlayerReaderThread;
import umg.util.CountryLookup;
import umg.util.DBUtil;
import umg.util.HTMLStreamUtils;
import umg.util.PropertyManager;
import umg.util.StringUtils;
import umg.util.XMLUtil;
import umg.vo.MatchSnapShotVO;
import umg.vo.PlayerVO;

public class GUI extends javax.swing.JFrame implements ActionListener, WindowListener, MouseListener, ChangeListener
{
	private JButton searchForNameButton;
	private JLabel jLabel4;// playerListControls;
	static private JTabbedPane playerListTabbedPane;
	public static JTabbedPane searchAndLogsTabbedPane;
	private JButton resetButton;
	public JEditorPane[] logsJEditorPane;
	private JScrollPane[] logsScrollPane;
	private JLabel jLabel6;
	private JButton searchForIPButton;
	private JLabel jLabel2;
	private static JTextArea statusTextArea;
	private static JTextArea searchResultsTextArea;
	private JTextField searchForIPTextField;
	private JTextField searchForNameTextField;
	MenuItem show;
	MenuItem exit, hide;
	private static TrayIcon trayIcon;
	private static final long serialVersionUID = -4217413583037701239L;
	private JMenuItem aboutMenuItem, databaseStatsMenuItem;
	private JMenu helpMenu;
	private JButton refreshButton;
	private JSeparator jSeparator1;
	private JLabel jLabel1;
	private static JTextField lastRefreshTextField;
	private JLabel jLabel3;
	public JLabel currentRefreshLabel;
	private JMenuItem refreshmenuItem;
	private JDesktopPane leftDesktopPane, rightDesktopPane;
	private JScrollPane playerListScrollPane[];
	private JScrollPane searchResultsScrollPane, scrollingStatusPanePane;
	private JSplitPane mainSplitPane;
	private JMenuItem configureMenuItem, addWatchIP, addWatchName, addAdminWatchIP,savePlayersToCrashListMenuItem, analyzeCrashListMenuItem;
	private JCheckBoxMenuItem proxyMenuItem, trayOnMinimizeMenuItem, chatEnabledMenuItem, autoKickPlayerMenuItem, startHiddenMenuItem;
	private static JCheckBoxMenuItem dontUpdateScreenMenuItem;
	public static JCheckBoxMenuItem databaseQueryModeMenuItem;
	private JMenu optionsMenu;
	private JMenuItem exitMenuItem;
	private JSeparator jSeparator2;
	private JButton jSendTextButton;
	private JTextField jSendTextField;
	public static RollOverJTable[] matchSnapShotTables;
	public static Object playerData[][][];// {server}{row}{col}
	private JMenu fileMenu;
	private JMenuBar menuBar;
	public String logs[];// actually displayed
	public String lastLines[] = null;
	private static DBUtil db = null;
	private static GUI inst;
	LinkedList<String> warnedPlayerIPList = null;
	static Image trayiconimage = null;
	ConfigureDialog configureDialog = null;
	static HashMap<String, String> lfmap;
	public static MatchSnapShotVO[] lastMatchData;
	final int MAX_SUSPECTS=10;


	// URL TO KICK
	// http://74.63.66.150:5080/ServerAdmin/current_players?[Kick|Ban]31=True

	static ArrayList<PlayerReaderThread> playerThreads;

	/**
	 * @param offline set param 1 to 'offline' to force the program to only two local file as input (players and chat).
	 * @author Code_Of_Life
	 */
	public static void main(String[] args)
	{
		// SwingUtilities.invokeLater(new RefreshTimerThread());//creates GUI
		inst = new GUI(args);//inits PropertyManager()
		inst.setLocationRelativeTo(null);
		playerThreads = new ArrayList<PlayerReaderThread>();
		lastMatchData= new MatchSnapShotVO[PropertyManager.getWebadminURLs().length];
		// now start one thread for each server.
		for (int i = 0; i < PropertyManager.getWebadminURLs().length; i++)
		{
			PlayerReaderThread thread = new PlayerReaderThread(i);
			thread.start();
			playerThreads.add(thread);

			ChatReaderThread chatThread = new ChatReaderThread(i, inst);
			chatThread.start();
		}
	}

	public GUI(String[] args)
	{
		super();

		new PropertyManager();
		if (args != null && args.length > 0 && args[0] != null && args[0].equalsIgnoreCase("offline"))
		{
			String[] webadminURLNames =
			{ "Offline" };
			String[] webadminURLs =
			{ "file:support_files/current_players.htm" };
			PropertyManager.setWebadminURLNames(webadminURLNames);
			PropertyManager.setWebadminURLs(webadminURLs);

			//not implemented for chats
		}
		new HTMLStreamUtils();
		setLookAndFeel();
		initGUI();

		//try initializing the GeoIP DB once
		CountryLookup countryLookup = new CountryLookup();
		String geoIPDBInfo = countryLookup.getDatabaseInfo();
		if (geoIPDBInfo == null) GUI.logToStatus("GeoIP Database NOT loaded.");
		else GUI.logToStatus(geoIPDBInfo);

		Toolkit.getDefaultToolkit().getSystemEventQueue().push(new umg.gui.MyEventQueue());

		if (PropertyManager.getStartMinimizedEnabled().equals(Messages.getString("GUI.56")))
		{
			this.setVisible(false);
		}
		else
		{
			this.setVisible(true);
		}

		// this.searchForNameTextField.requestFocusInWindow();
	}

	private void initGUI()
	{
		try
		{
			setSize(800, 600);
			setResizable(false);
			this.addWindowListener(this);
			this.setTitle(Constants.APP_NAME);
			URL logoURL = getClass().getResource(Constants.ICON_IMAGE);
			this.setIconImage(Toolkit.getDefaultToolkit().getImage(logoURL));
			mainSplitPane = new JSplitPane();
			getContentPane().add(mainSplitPane, BorderLayout.CENTER);
			mainSplitPane.setDividerLocation(400);
			mainSplitPane.setPreferredSize(new java.awt.Dimension(790, 512));
			// left pain
			leftDesktopPane = new JDesktopPane();
			mainSplitPane.add(leftDesktopPane, JSplitPane.LEFT);
			leftDesktopPane.setPreferredSize(new java.awt.Dimension(397, 590));
			leftDesktopPane.setBackground(new Color(238, 238, 238));

			// right pane
			rightDesktopPane = new JDesktopPane();
			rightDesktopPane.setPreferredSize(new java.awt.Dimension(397, 590));
			rightDesktopPane.setBackground(new Color(238, 238, 238));
			mainSplitPane.add(rightDesktopPane, JSplitPane.RIGHT);
			mainSplitPane.setEnabled(false);

			searchAndLogsTabbedPane = new JTabbedPane();
			searchAndLogsTabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
			// searchAndLogsTabbedPane.setTabPlacement(JTabbedPane.LEFT);

			// status tab is added first, followed by search results.
			statusTextArea = new JTextArea();
			statusTextArea.setBackground(new Color(36, 57, 84));
			statusTextArea.setForeground(Color.WHITE);
			statusTextArea.setFont(new java.awt.Font(Messages.getString("GUI.7"), java.awt.Font.PLAIN, 11));
			scrollingStatusPanePane = new JScrollPane(statusTextArea);
			// scrollingStatusPanePane.setBounds(0, 440, 399, 113);
			// statusTextArea.setBounds(14, 455, 373, 95);
			scrollingStatusPanePane.setAutoscrolls(true);
			// statusTextArea.setPreferredSize(new java.awt.Dimension(381,
			// 70020));
			statusTextArea.setEditable(false);
			// statusTextArea.setRows(5000);
			statusTextArea.setWrapStyleWord(true);
			// statusTextArea.setLineWrap(true);
			scrollingStatusPanePane.setViewportView(statusTextArea);
			// scrollingStatusPanePane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
			// );
			// leftDesktopPane.add(scrollingStatusPanePane);
			searchAndLogsTabbedPane.addTab(Messages.getString("GUI.8"), scrollingStatusPanePane);
			scrollingStatusPanePane.setPreferredSize(new java.awt.Dimension(394, 373));

			searchAndLogsTabbedPane.addChangeListener(this);
			leftDesktopPane.add(searchAndLogsTabbedPane);
			searchAndLogsTabbedPane.setBounds(0, 102, 399, 426);
			// search results
			// searchResultsTextArea = new
			// JTextAreaWithBackground(Constants.UMG_LOGO_IMAGE);
			searchResultsTextArea = new JTextArea();
			searchResultsTextArea.setBackground(new Color(36, 57, 84));
			searchResultsTextArea.setForeground(Color.WHITE);
			searchResultsTextArea.setFont(new java.awt.Font(Messages.getString("GUI.1"), java.awt.Font.PLAIN, 12));
			// searchResultsTextArea.setForeground(new java.awt.Color(255, 255,
			// 255));
			// leftDesktopPane.add(searchResultsTextArea);
			// searchResultsTextArea.setBounds(12, 179, 48, -10);
			searchResultsTextArea.setEditable(false);
			// searchResultsTextArea.setRows(200);
			searchResultsTextArea.setLineWrap(true);
			// searchResultsTextArea.setPreferredSize(new
			// java.awt.Dimension(376, 390));
			searchResultsTextArea.setWrapStyleWord(true);
			// searchResultsScrollPane.setViewportView(searchResultsTextArea);
			// searchResultsTextArea.setPreferredSize(new
			// java.awt.Dimension(336, 2810));
			searchResultsScrollPane = new JScrollPane(searchResultsTextArea);
			searchResultsScrollPane.setAutoscrolls(true);
			searchAndLogsTabbedPane.addTab(Messages.getString("GUI.2"), searchResultsScrollPane);
			searchResultsScrollPane.setPreferredSize(new java.awt.Dimension(394, 402));
			// searchResultsScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
			// );

			// add tabs for the consoles of each of the server urls
			logsJEditorPane = new JEditorPane[PropertyManager.getWebadminURLs().length];
			logsScrollPane = new JScrollPane[PropertyManager.getWebadminURLs().length];

			logs = new String[PropertyManager.getWebadminURLs().length];
			TableModel emptyTableModel = new DefaultTableModel(new String[][]
			{ {} }, Constants.TABLE_HEADER);/*
											* data,
											* columnnames
											*/
			// add the player list tabs
			matchSnapShotTables = new RollOverJTable[PropertyManager.getWebadminURLs().length];
			playerData = new Object[PropertyManager.getWebadminURLs().length][Constants.MAXPLAYERS][Constants.TABLE_HEADER.length];
			playerListTabbedPane = new JTabbedPane();
			playerListTabbedPane.addChangeListener(this);

			// create the JTables, add one to a new tab
			playerListScrollPane = new JScrollPane[matchSnapShotTables.length];
			for (int i = 0; i < PropertyManager.getWebadminURLs().length; i++)
			{
				logs[i] = new String(Messages.getString("GUI.3"));
				matchSnapShotTables[i] = new RollOverJTable(emptyTableModel);
				//team color, aliasing players
				matchSnapShotTables[i].setDefaultRenderer(String.class, new CustomTableCellRenderer());
				matchSnapShotTables[i].setForeground(new java.awt.Color(255, 255, 255));
				matchSnapShotTables[i].setBackground(new java.awt.Color(36, 57, 84));

				//TableColumn col = matchSnapShotTables[i].getColumnModel().getColumn(1);
				//col.setCellRenderer(new JLabelTableCellRenderer());

				JTableHeader header = matchSnapShotTables[i].getTableHeader();
				header.setBackground(Color.white);
				header.setForeground(new Color(36, 57, 84));
				// if(i>0)matchSnapShotTables[i].setVisible(false);
				matchSnapShotTables[i].setAutoCreateRowSorter(true);
				setColumnWidthsForJTable(matchSnapShotTables[i]);

				// create a scroll pane containing a table
				playerListScrollPane[i] = new JScrollPane(matchSnapShotTables[i]);
				playerListScrollPane[i].getViewport().setBackground(new java.awt.Color(36, 57, 84));// initial
				// add the pane to a new tab
				playerListTabbedPane.addTab(PropertyManager.getWebadminURLNames()[i], playerListScrollPane[i]);

				// log viewer tabs
				logsJEditorPane[i] = new JEditorPane(Messages.getString("GUI.4"), Messages.getString("GUI.3"));
				logsJEditorPane[i].setBackground(new java.awt.Color(36, 57, 84));
				logsScrollPane[i] = new JScrollPane(logsJEditorPane[i]);
				logsJEditorPane[i].setAutoscrolls(true);
				// logsScrollPane[i].setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
				// );
				searchAndLogsTabbedPane.addTab(PropertyManager.getWebadminURLNames()[i] + Messages.getString("GUI.6"), logsScrollPane[i]);

			}// for all urls
				// tab for logs
				// scrolling status messages
				// statusTextArea = new
				// JTextAreaWithBackground(Constants.UMG_LOGO_IMAGE);
				// add the JTabbed pane to the rightDesktopPane
				// playerListControls=new JLabel();
				// playerListControls.setFont(new java.awt.Font("Dialog",
				// java.awt.Font.PLAIN, 10));
				// playerListControls.setBounds(rightDesktopPane.getX(),
				// rightDesktopPane.getY(), rightDesktopPane.getWidth(), 17);
				// rightDesktopPane.add(playerListControls);
				// playerListControls.setBounds(0, 0, 386, 16);
			rightDesktopPane.add(playerListTabbedPane);
			playerListTabbedPane.setBounds(0, 0, 386, 510);
			// playerListTabbedPane.setSize(new
			// Dimension(rightDesktopPane.getPreferredSize().width-15,rightDesktopPane.getPreferredSize().height-playerListControls.getHeight()-41));
			playerListTabbedPane.setSize(new Dimension(rightDesktopPane.getPreferredSize().width - 15, rightDesktopPane.getPreferredSize().height - 41));

			// tray menu
			if (SystemTray.isSupported())
			{
				PopupMenu menu = new PopupMenu();
				show = new MenuItem(Messages.getString("GUI.9"));
				show.setActionCommand(Messages.getString("GUI.9"));
				show.addActionListener(this);
				hide = new MenuItem(Messages.getString("GUI.11"));
				hide.setActionCommand(Messages.getString("GUI.11"));
				hide.addActionListener(this);
				exit = new MenuItem(Messages.getString("GUI.13"));
				exit.setActionCommand(Messages.getString("GUI.13"));
				exit.addActionListener(this);
				menu.add(show);
				menu.add(hide);
				menu.addSeparator();
				menu.add(exit);
				try
				{
					SystemTray tray = SystemTray.getSystemTray();
					URL urlImage1 = getClass().getResource(Constants.ICON_IMAGE);
					if (urlImage1 == null) throw new Exception();
					if (trayiconimage == null) trayiconimage = Toolkit.getDefaultToolkit().getImage(urlImage1);

					trayIcon = new TrayIcon(trayiconimage, Constants.APP_NAME, menu);
					trayIcon.setImageAutoSize(true);
					trayIcon.setActionCommand(Messages.getString("GUI.15"));
					trayIcon.addActionListener(this);
					trayIcon.addMouseListener(this);

					tray.add(trayIcon);
					// trayIcon.displayMessage("Event","MSG",TrayIcon.MessageType.INFO);
				}
				catch (Exception e)//AWTException
				{
					e.printStackTrace();
					System.err.println(Messages.getString("GUI.16") + Messages.getString("GUI.71"));
					System.exit(-1);
				}

			}
			else
			{
				System.out.println(Messages.getString("GUI.17"));
			}

			{
				refreshButton = new JButton();
				leftDesktopPane.add(refreshButton);
				refreshButton.setText(Messages.getString("GUI.18"));
				refreshButton.setFont(new java.awt.Font(Messages.getString("GUI.19"), 0, 11));
				refreshButton.setToolTipText(Messages.getString("GUI.20"));
				refreshButton.setBounds(272, 5, 123, 21);
				refreshButton.addActionListener(this);
				refreshButton.setVisible(true);
				refreshButton.setEnabled(true);
			}
			{
				currentRefreshLabel = new JLabel(Messages.getString("GUI.21"));
				leftDesktopPane.add(currentRefreshLabel);
				currentRefreshLabel.setBounds(98, 28, 26, 17);
				// currentRefreshLabel.setEditable(false);
				// currentRefreshLabel.setAutoscrolls(false);
				currentRefreshLabel.setFont(new java.awt.Font(Messages.getString("GUI.22"), 0, 13));
				currentRefreshLabel.setToolTipText(Messages.getString("GUI.23"));
				currentRefreshLabel.setText(PropertyManager.getGrab_interval());
			}
			{
				jLabel3 = new JLabel();
				jLabel3.setFont(new java.awt.Font(Messages.getString("GUI.22"), 1, 10));
				leftDesktopPane.add(jLabel3);
				jLabel3.setText(Messages.getString("GUI.25"));
				jLabel3.setBounds(4, 29, 106, 14);
			}
			{
				lastRefreshTextField = new JTextField();
				leftDesktopPane.add(lastRefreshTextField);
				lastRefreshTextField.setBounds(103, 5, 167, 21);
				lastRefreshTextField.setHorizontalAlignment(JTextField.CENTER);
				lastRefreshTextField.setEditable(false);
			}
			{
				jLabel1 = new JLabel();
				leftDesktopPane.add(jLabel1);
				jLabel1.setText(Messages.getString("GUI.26"));
				jLabel1.setBounds(4, 8, 106, 14);
			}

			jSeparator1 = new JSeparator();
			leftDesktopPane.add(jSeparator1);
			jSeparator1.setBounds(-1, 43, 401, 8);

			searchForNameTextField = new JTextField(Messages.getString("GUI.27"));
			searchForNameTextField.setCaretPosition(1);
			leftDesktopPane.add(searchForNameTextField);
			searchForNameTextField.setBounds(104, 49, 167, 21);
			searchForNameTextField.setToolTipText(Messages.getString("GUI.28"));

			searchForIPTextField = new JTextField(Messages.getString("GUI.29"));
			searchForIPTextField.setCaretPosition(0);
			leftDesktopPane.add(searchForIPTextField);
			searchForIPTextField.setBounds(104, 76, 167, 21);
			searchForIPTextField.setToolTipText(Messages.getString("GUI.28"));

			searchForNameButton = new JButton();
			leftDesktopPane.add(searchForNameButton);
			searchForNameButton.setText(Messages.getString("GUI.31"));
			searchForNameButton.setActionCommand(Messages.getString("GUI.31"));
			searchForNameButton.addActionListener(this);
			searchForNameButton.setBounds(272, 49, 123, 21);
			searchForNameButton.setFont(new java.awt.Font(Messages.getString("GUI.22"), 0, 11));

			jLabel2 = new JLabel();
			leftDesktopPane.add(jLabel2);
			jLabel2.setText(Messages.getString("GUI.34"));
			jLabel2.setBounds(3, 54, 106, 14);

			jLabel4 = new JLabel();
			leftDesktopPane.add(jLabel4);
			jLabel4.setText(Messages.getString("GUI.35"));
			jLabel4.setBounds(3, 82, 106, 14);

			searchForIPButton = new JButton();
			leftDesktopPane.add(searchForIPButton);
			searchForIPButton.setText(Messages.getString("GUI.36"));
			searchForIPButton.setActionCommand(Messages.getString("GUI.36"));
			searchForIPButton.addActionListener(this);
			searchForIPButton.setBounds(272, 76, 123, 21);
			searchForIPButton.setFont(new java.awt.Font(Messages.getString("GUI.22"), 0, 11));

			jLabel6 = new JLabel();
			jLabel6.setFont(new java.awt.Font(Messages.getString("GUI.22"), 1, 10));
			leftDesktopPane.add(jLabel6);
			jLabel6.setText(Messages.getString("GUI.40"));
			jLabel6.setBounds(121, 30, 71, 13);

			resetButton = new JButton();
			leftDesktopPane.add(resetButton);
			resetButton.setText(Messages.getString("GUI.41"));
			resetButton.setVisible(true);
			resetButton.addActionListener(this);
			resetButton.setFont(new java.awt.Font(Messages.getString("GUI.42"), 0, 11));
			resetButton.setToolTipText(Messages.getString("GUI.43"));

			resetButton.setBounds(272, 28, 123, 13);
			{
				jSendTextButton = new JButton();
				leftDesktopPane.add(jSendTextButton);
				jSendTextButton.setText(Messages.getString("GUI.44"));
				jSendTextButton.setBounds(334, 527, 65, 14);
				jSendTextButton.setSize(65, 19);
				jSendTextButton.setActionCommand(Messages.getString("GUI.45"));
				jSendTextButton.addActionListener(this);
			}
			{
				jSendTextField = new JTextField();
				leftDesktopPane.add(jSendTextField);
				jSendTextField.setBounds(0, 528, 333, 14);
				jSendTextField.setSize(335, 19);
			}

			// menu bar
			menuBar = new JMenuBar();
			setJMenuBar(menuBar);
			fileMenu = new JMenu();
			//TODO: Add "Export database to SQL file"
			menuBar.add(fileMenu);
			fileMenu.setText(Messages.getString("GUI.46"));
			{
				refreshmenuItem = new JMenuItem();
				fileMenu.add(refreshmenuItem);
				refreshmenuItem.setText(Messages.getString("GUI.47"));
				refreshmenuItem.addActionListener(this);
			}
			{
				jSeparator2 = new JSeparator();
				fileMenu.add(jSeparator2);
			}
			{
				exitMenuItem = new JMenuItem();
				fileMenu.add(exitMenuItem);
				exitMenuItem.setText(Messages.getString("GUI.49"));
				exitMenuItem.addActionListener(this);
			}

			optionsMenu = new JMenu();
			menuBar.add(optionsMenu);
			optionsMenu.setText(Messages.getString("GUI.51"));
			optionsMenu.addActionListener(this);
			configureMenuItem = new JMenuItem();
			optionsMenu.add(configureMenuItem);
			configureMenuItem.setText(Messages.getString("GUI.52"));
			configureMenuItem.addActionListener(this);

			addWatchIP = new JMenuItem();
			optionsMenu.add(addWatchIP);
			addWatchIP.setText(Messages.getString("GUI.53"));
			addWatchIP.addActionListener(this);

			addWatchName = new JMenuItem();
			optionsMenu.add(addWatchName);
			addWatchName.setText(Messages.getString("GUI.54"));
			addWatchName.addActionListener(this);

			addAdminWatchIP = new JMenuItem();
			optionsMenu.add(addAdminWatchIP);
			addAdminWatchIP.setText(Messages.getString("GUI.55"));
			addAdminWatchIP.addActionListener(this);
			
			savePlayersToCrashListMenuItem = new JMenuItem();
			optionsMenu.add(savePlayersToCrashListMenuItem);
			savePlayersToCrashListMenuItem.setText(Messages.getString("GUI.200"));
			//savePlayersToCrashListMenuItem.setForeground(Color.RED);
			//savePlayersToCrashListMenuItem.setBackground(Color.BLACK);
			savePlayersToCrashListMenuItem.addActionListener(this);
			
			analyzeCrashListMenuItem = new JMenuItem();
			optionsMenu.add(analyzeCrashListMenuItem);
			analyzeCrashListMenuItem.setText(Messages.getString("GUI.201"));
			//analyzeCrashListMenuItem.setForeground(Color.GREEN);
			//analyzeCrashListMenuItem.setBackground(Color.BLACK);
			analyzeCrashListMenuItem.addActionListener(this);
			optionsMenu.addSeparator();
			proxyMenuItem = new JCheckBoxMenuItem();
			proxyMenuItem.setSelected(PropertyManager.getProxy_enabled().equalsIgnoreCase(Messages.getString("GUI.56")));
			optionsMenu.add(proxyMenuItem);
			proxyMenuItem.setText(Messages.getString("GUI.57"));
			proxyMenuItem.addActionListener(this);

			chatEnabledMenuItem = new JCheckBoxMenuItem();
			chatEnabledMenuItem.setSelected(PropertyManager.getChat_enabled().equalsIgnoreCase(Messages.getString("GUI.56")));
			optionsMenu.add(chatEnabledMenuItem);
			chatEnabledMenuItem.setText(Messages.getString("GUI.59"));
			chatEnabledMenuItem.addActionListener(this);

			trayOnMinimizeMenuItem = new JCheckBoxMenuItem();
			trayOnMinimizeMenuItem.setSelected(PropertyManager.getMinimize_to_tray_enabled().equalsIgnoreCase(Messages.getString("GUI.56")));
			optionsMenu.add(trayOnMinimizeMenuItem);
			trayOnMinimizeMenuItem.setText(Messages.getString("GUI.61"));
			trayOnMinimizeMenuItem.addActionListener(this);

			dontUpdateScreenMenuItem = new JCheckBoxMenuItem();
			dontUpdateScreenMenuItem.setSelected(!PropertyManager.getUpdateScreen().equalsIgnoreCase(Messages.getString("GUI.56")));
			optionsMenu.add(dontUpdateScreenMenuItem);
			dontUpdateScreenMenuItem.setText(Messages.getString("GUI.63"));
			dontUpdateScreenMenuItem.addActionListener(this);

			databaseQueryModeMenuItem = new JCheckBoxMenuItem();
			databaseQueryModeMenuItem.setSelected(!PropertyManager.getDatabaseModeOnly().equalsIgnoreCase(Messages.getString("GUI.113")));
			if (databaseQueryModeMenuItem.isSelected())
			{
				logToStatus(Messages.getString("GUI.184"));
			}
			optionsMenu.add(databaseQueryModeMenuItem);
			databaseQueryModeMenuItem.setText(Messages.getString("GUI.64"));
			databaseQueryModeMenuItem.addActionListener(this);

			autoKickPlayerMenuItem = new JCheckBoxMenuItem();
			if (PropertyManager.getAutokickPlayerEnabled().equalsIgnoreCase(Messages.getString("GUI.56")))
			{
				autoKickPlayerMenuItem.setSelected(true);
			}
			else
			{
				autoKickPlayerMenuItem.setSelected(false);
			}
			optionsMenu.add(autoKickPlayerMenuItem);
			autoKickPlayerMenuItem.setText(Messages.getString("GUI.66"));
			autoKickPlayerMenuItem.addActionListener(this);

			startHiddenMenuItem = new JCheckBoxMenuItem();
			if (PropertyManager.getStartMinimizedEnabled().equalsIgnoreCase(Messages.getString("GUI.56")))
			{
				startHiddenMenuItem.setSelected(true);
			}
			else
			{
				startHiddenMenuItem.setSelected(false);
			}
			optionsMenu.add(startHiddenMenuItem);
			startHiddenMenuItem.setText(Messages.getString("GUI.186"));
			startHiddenMenuItem.addActionListener(this);

			helpMenu = new JMenu();
			menuBar.add(helpMenu);
			helpMenu.setText(Messages.getString("GUI.67"));
			{
				aboutMenuItem = new JMenuItem();
				helpMenu.add(aboutMenuItem);
				aboutMenuItem.setText(Messages.getString("GUI.68"));
				aboutMenuItem.addActionListener(this);
				databaseStatsMenuItem = new JMenuItem();
				helpMenu.add(databaseStatsMenuItem);
				databaseStatsMenuItem.setText(Messages.getString("GUI.69"));
				databaseStatsMenuItem.addActionListener(this);
			}
			// this.addWindowListener(new MainFrameListener());
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void setLookAndFeel()
	{
		UIManager.LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();
		if (lfmap == null)
		{
			lfmap = new HashMap<String, String>(10);
			for (int i = 0; i < plafs.length; i++)
			{
				if (PropertyManager.isDebug()) System.out.println("L&F: " + plafs[i].getName() + " Class: " + plafs[i].getClassName());
				lfmap.put(plafs[i].getName(), plafs[i].getClassName());
			}
		}
		try
		{
			UIManager.setLookAndFeel(lfmap.get(PropertyManager.getSelectedSkin()));
		}
		catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (InstantiationException e)
		{
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		catch (UnsupportedLookAndFeelException e)
		{
			e.printStackTrace();
		}
		//GUI.logToStatus("Skin set to '"+PropertyManager.getSelectedSkin()+"'");
		//UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	}

	public static void setColumnWidthsForJTable(RollOverJTable rollOverJTable)
	{
		try
		// set column widths
		{
			rollOverJTable.getColumnModel().getColumn(0).setPreferredWidth(120);//name
			rollOverJTable.getColumnModel().getColumn(1).setPreferredWidth(25);//country
			rollOverJTable.getColumnModel().getColumn(2).setPreferredWidth(35);//ping
			rollOverJTable.getColumnModel().getColumn(3).setPreferredWidth(35);//score
			rollOverJTable.getColumnModel().getColumn(4).setPreferredWidth(105);//ip
			rollOverJTable.getColumnModel().getColumn(5).setPreferredWidth(40);//team
			//hide webid
			rollOverJTable.getColumnModel().getColumn(6).setMaxWidth(0);
			rollOverJTable.getColumnModel().getColumn(6).setMinWidth(0);
			rollOverJTable.getColumnModel().getColumn(6).setWidth(0);
			rollOverJTable.getColumnModel().getColumn(6).setPreferredWidth(0);
		}
		catch (Exception e)
		{/* e.printStackTrace(); */
		}

	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (db == null) db = new DBUtil();
		if (e.getActionCommand().equals(Messages.getString("GUI.70")))
		{
			System.out.println(Messages.getString("GUI.71"));
			db.disconnect();
			System.exit(0);
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.31")))
		{
			// get matches from the db, populate the textarea
			if (searchForNameTextField.getText().trim().length() == 0 || searchForNameTextField.getText().trim().equals(Messages.getString("GUI.27")))
			{
				JOptionPane.showMessageDialog(this, Messages.getString("GUI.74"), Messages.getString("GUI.75"), JOptionPane.WARNING_MESSAGE);
			}
			else
			{// search
				searchResultsTextArea.setText(db.searchByName(searchForNameTextField.getText().trim()));
				searchResultsTextArea.setCaretPosition(0);
				GUI.searchAndLogsTabbedPane.setSelectedIndex(1);
				GUI.logToStatus("Searched for Name \"" + searchForNameTextField.getText().trim() + "\"");
			}
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.36")))
		{
			// get matches from the db, populate the textarea
			if (searchForIPTextField.getText().trim().length() == 0 || searchForIPTextField.getText().trim().equals(Messages.getString("GUI.77")))
			{
				JOptionPane.showMessageDialog(this, Messages.getString("GUI.78"), Messages.getString("GUI.79"), JOptionPane.WARNING_MESSAGE);
			}
			else
			{// search
				searchResultsTextArea.setText(db.searchByIP(searchForIPTextField.getText().trim()));
				searchResultsTextArea.setCaretPosition(0);
				GUI.searchAndLogsTabbedPane.setSelectedIndex(1);
				GUI.logToStatus("Searched for IP \"" + searchForIPTextField.getText().trim() + "\"");
			}
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.80")))
		{
			// send text only if we are in a tab connected to a URL
			if (searchAndLogsTabbedPane.getSelectedIndex() > 1)
			{
				if (jSendTextField.getText().trim().length() > 0)
				{
					HTMLStreamUtils.sendText(searchAndLogsTabbedPane.getSelectedIndex() - 2, jSendTextField.getText());
				}
				jSendTextField.setText(Messages.getString("GUI.3"));
			}
			else
			{
				JOptionPane.showMessageDialog(this, Messages.getString("GUI.82"), Messages.getString("GUI.83"), JOptionPane.WARNING_MESSAGE);
			}

		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.84")))
		{
			configureDialog = null;
			configureDialog = new ConfigureDialog(this);
			configureDialog.setLocationRelativeTo(this); //make dialog modal and centered.
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.87")))
		{
			// new ConfigureDialog(new JFrame());
			String name = (String) JOptionPane.showInputDialog(this, Messages.getString("GUI.88"), Messages.getString("GUI.89"), JOptionPane.INFORMATION_MESSAGE, null, null, Messages.getString("GUI.3"));
			if (name != null && name.length() > 0)
			{
				String nameList = PropertyManager.getNameWatchList();
				if (nameList.trim().length() > 0) nameList += Constants.NAME_LIST_SEPERATOR;
				nameList += name;
				PropertyManager.setNameWatchList(nameList);
				PropertyManager.saveToDisk();
				logToStatus(Messages.getString("GUI.91") + name + Messages.getString("GUI.92"));
			}
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.93")))
		{
			// new ConfigureDialog(new JFrame());
			String ip = (String) JOptionPane.showInputDialog(this, Messages.getString("GUI.94"), Messages.getString("GUI.95"), JOptionPane.INFORMATION_MESSAGE, null, null, Messages.getString("GUI.3"));
			if (ip != null && ip.length() > 2)
			{
				String ipList = PropertyManager.getIPWatchList();
				if (ipList.trim().length() > 0) ipList += Constants.IP_LIST_SEPERATOR;
				ipList += ip;
				PropertyManager.setIPWatchList(ipList);
				PropertyManager.saveToDisk();
				logToStatus(Messages.getString("GUI.97") + ip + Messages.getString("GUI.98"));
			}
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.99")))
		{
			// new ConfigureDialog(new JFrame());
			String ip = (String) JOptionPane.showInputDialog(this, Messages.getString("GUI.100"), Messages.getString("GUI.101"), JOptionPane.INFORMATION_MESSAGE, null, null, Messages.getString("GUI.3"));
			if (ip != null && ip.length() > 2)
			{
				String ipList = PropertyManager.getAdminIPAddresses();
				if (ipList.trim().length() > 0) ipList += Constants.IP_LIST_SEPERATOR;
				ipList += ip;
				PropertyManager.setAdminIPAddresses(ipList);
				PropertyManager.saveToDisk();
				logToStatus(Messages.getString("GUI.103") + ip);
			}
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.104")))
		{
			this.setVisible(true);
			refreshSelectedTab();
			this.setExtendedState(NORMAL);// restore if minimized
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.105")))
		{
			this.setVisible(false);
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.106")))
		{
			searchForIPTextField.setText(Messages.getString("GUI.107"));
			searchForIPTextField.setCaretPosition(0);
			searchForNameTextField.setText(Messages.getString("GUI.27"));
			searchForNameTextField.setCaretPosition(1);
			searchResultsTextArea.setText(Messages.getString("GUI.3"));
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.110")))
		{
			// this.setVisible(false);
			if (proxyMenuItem.isSelected())
			{
				PropertyManager.setProxy_enabled(Messages.getString("GUI.56"));
				logToStatus(Messages.getString("GUI.112"));
			}
			else
			{
				PropertyManager.setProxy_enabled(Messages.getString("GUI.113"));
				logToStatus(Messages.getString("GUI.114"));
			}
			PropertyManager.saveToDisk();
			// getDataRefreshTablesAndSave(System.currentTimeMillis());
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.115")))
		{
			// this.setVisible(false);
			if (chatEnabledMenuItem.isSelected())
			{
				PropertyManager.setChat_enabled(Messages.getString("GUI.56"));
				logToStatus(Messages.getString("GUI.117"));
			}
			else
			{
				PropertyManager.setChat_enabled(Messages.getString("GUI.113"));
				logToStatus(Messages.getString("GUI.119"));
			}
			PropertyManager.saveToDisk();
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.120")))
		{
			// this.setVisible(false);
			if (dontUpdateScreenMenuItem.isSelected())
			{
				PropertyManager.setUpdateScreen(Messages.getString("GUI.113"));
				logToStatus(Messages.getString("GUI.122"));
			}
			else
			{
				PropertyManager.setUpdateScreen(Messages.getString("GUI.56"));
				logToStatus(Messages.getString("GUI.124"));
			}
			PropertyManager.saveToDisk();
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.64")))
		{
			// this.setVisible(false);
			if (databaseQueryModeMenuItem.isSelected())
			{
				databaseQueryModeMenuItem.setSelected(true);
				PropertyManager.setDatabaseModeOnly(Messages.getString("GUI.56"));
				logToStatus(Messages.getString("GUI.184"));
			}
			else
			{
				databaseQueryModeMenuItem.setSelected(false);
				PropertyManager.setDatabaseModeOnly(Messages.getString("GUI.113"));
				logToStatus(Messages.getString("GUI.185"));
			}
			PropertyManager.saveToDisk();
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.125")))
		{
			// this.setVisible(false);
			if (trayOnMinimizeMenuItem.isSelected())
			{
				PropertyManager.setMinimize_to_tray_enabled(Messages.getString("GUI.56"));
				logToStatus(Messages.getString("GUI.127"));
			}
			else
			{
				PropertyManager.setMinimize_to_tray_enabled(Messages.getString("GUI.113"));
				logToStatus(Messages.getString("GUI.129"));
			}
			PropertyManager.saveToDisk();
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.130")))
		{
			// this.setVisible(false);
			if (autoKickPlayerMenuItem.isSelected())
			{
				PropertyManager.setAutokickPlayerEnabled(Messages.getString("GUI.56"));
				logToStatus(Messages.getString("GUI.132"));
			}
			else
			{
				PropertyManager.setAutokickPlayerEnabled(Messages.getString("GUI.113"));
				logToStatus(Messages.getString("GUI.134"));
			}
			PropertyManager.saveToDisk();
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.186")))
		{
			// this.setVisible(false);
			if (autoKickPlayerMenuItem.isSelected())
			{
				PropertyManager.setStartMinimizedEnabled(Messages.getString("GUI.56"));// false
				logToStatus(Messages.getString("GUI.187"));
			}
			else
			{
				PropertyManager.setStartMinimizedEnabled(Messages.getString("GUI.113"));
				logToStatus(Messages.getString("GUI.188"));
			}
			PropertyManager.saveToDisk();
		}
		// else if (e.getActionCommand().startsWith("Radio"))
		// {
		// //set visible the table which corresponds to this radio buttons
		// actioncommand.
		// int tableToShow= Integer.parseInt(e.getActionCommand().substring(5));
		//
		// for(int i=0;i<matchSnapShotTables.length;i++)
		// {
		// if(i==tableToShow)matchSnapShotTables[i].setVisible(true);
		// else matchSnapShotTables[i].setVisible(false);
		// }
		// matchScrollpane = new JScrollPane(matchSnapShotTables[tableToShow]);
		// mainSplitPane.add(matchScrollpane, JSplitPane.RIGHT);
		// matchScrollpane.getViewport().setBackground(new
		// java.awt.Color(36,57,84));
		// matchScrollpane.setPreferredSize(new java.awt.Dimension(358, 517));
		//
		// }
		else if (e.getActionCommand().equals(Messages.getString("GUI.135")))// tray
		// left
		// click toggles
		// visibility
		{
			// if (this.isVisible())
			// this.setVisible(false);
			// else
			// {
			// this.setVisible(true);
			// this.setExtendedState(NORMAL);//restore if minimized
			// }
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.136")))
		{
			JOptionPane.showMessageDialog(this, Constants.VERSION_STR + Messages.getString("GUI.137"), Messages.getString("GUI.138"), JOptionPane.INFORMATION_MESSAGE);
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.139")))
		{
			if (db == null) db = new DBUtil();
			JOptionPane.showMessageDialog(this, db.getDBStats(), Messages.getString("GUI.139"), JOptionPane.INFORMATION_MESSAGE);
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.141")))
		{
			// getDataRefreshTablesAndSave(System.currentTimeMillis());
			int displayedTabIndex = playerListTabbedPane.getSelectedIndex();
			logToStatus(Messages.getString("GUI.142") + (playerListTabbedPane.getSelectedIndex() + 1) + Messages.getString("GUI.143"));
			playerThreads.get(displayedTabIndex).getDataRefreshTablesAndSave(System.currentTimeMillis());
		}
		else if (e.getActionCommand().equals(Messages.getString("GUI.200")))
		{
			saveMatchToCrashlist();
		}	
		else if (e.getActionCommand().equals(Messages.getString("GUI.201")))
		{
			analyzeCrashData(null);//null makes it read the xml file.
		}			
		else if (e.getActionCommand().equals(Messages.getString("GUI.146")))
		{
			PropertyManager.saveToDisk();
			db.disconnect();
			if (PropertyManager.isDebug()) System.out.println(Messages.getString("GUI.147"));
			System.exit(0);
		}
		else
		{
			if (PropertyManager.isDebug())
			{
				System.out.println(Messages.getString("GUI.148") + e);
			}
		}
	}

	private void saveMatchToCrashlist()
	{
		int url_number = GUI.playerListTabbedPane.getSelectedIndex();
		MatchSnapShotVO match=lastMatchData[url_number];
		
		System.out.println("Reading previous crash data...");
		ArrayList<MatchSnapShotVO> matches=XMLUtil.readXmlFile("crashedMatches.xml");
		if(matches==null)matches=new ArrayList<MatchSnapShotVO>();

		matches.add(match);
		System.out.println("Writing match data...");
		XMLUtil.saveXmlFile("crashedMatches.xml", matches);
		logToStatus("Match data saved to crashedMatches.xml, match count = "+matches.size());
	}

	/**
	 * go through the match data and find the frequency of subnets.
	 * display the top suspects for
	 * a) all matches
	 * b) players in the last crash
	 * @param matches
	 */
	private void analyzeCrashData(ArrayList<MatchSnapShotVO> matches)
	{
		if(matches==null)
		{
			//read the file
			System.out.println("Reading previous crash data...");
			matches=XMLUtil.readXmlFile("crashedMatches.xml");
		}
		if(matches==null)
		{
			System.out.println("Error: No match data to analyze.");
			return;
		}
		HashMap<String,Long> subnetFreqHash= new HashMap<String,Long>();
		MatchSnapShotVO lastMatch=null;
		for(MatchSnapShotVO match:matches)
		{
			lastMatch=match;
			PlayerVO[] players= match.getPlayers();
			String subnet=null;
			for(PlayerVO player:players)
			{
				subnet=StringUtils.getSubnet(player.getIP());
				if(subnetFreqHash.get(subnet)!=null)
				{
					subnetFreqHash.put(subnet, subnetFreqHash.get(subnet)+1);
				}
				else
				{
					subnetFreqHash.put(subnet, new Long(1));
				}
			}
		}//for loop 
		
		//sort the hashmap by count;
		ArrayList<Long> sortedCounts=new ArrayList<Long>(subnetFreqHash.values());
		Collections.sort(sortedCounts);
		
		//create a list of suspect subnets in decresing order
		ArrayList<String> sortedSuspectSubnets= new ArrayList<String>();
		sortedCounts=new ArrayList<Long>(new HashSet<Long>(sortedCounts));//remove duplicates
		Set<String> subnetKeys=subnetFreqHash.keySet();
		try
		{
		for(int i=sortedCounts.size()-1;i>=0;i--)
		{
			//find the subnet in the hashmap corresponding to this count, Add it to the new List.
			Iterator<String> iter=subnetKeys.iterator();
			while(iter.hasNext())
			{
				String subnetKey=iter.next();
				if(subnetFreqHash.get(subnetKey)==sortedCounts.get(i))
				{
					sortedSuspectSubnets.add(subnetKey);
					//no break here, there may be many subnets with the same count.
				}
			}
		}//sorting for
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		//display the results.
		StringBuffer sb= new StringBuffer();//needed as we should all at once, othersise it shows up backwards)
		sb.append("*** Crash matches analysis ***\n");
		sb.append("Global Suspect subnets (top "+MAX_SUSPECTS+"):");
		int count=0;
		for(String subnetKey:sortedSuspectSubnets)
		{
			count++;
			sb.append("\n\tSubnet: "+subnetKey+"  Frequency: "+subnetFreqHash.get(subnetKey));
			//commented as it spits out too much data
			//if (db == null) db = new HSQLDBUtil();
			//sb.append(db.searchByIP(subnetKey+"%"));
			if(count>=MAX_SUSPECTS) break;
		}
		//top 3 suspects in the last match
		sb.append("\nLast Match players subnet crash frequency:\n");
		PlayerVO[] lastMatchPlayers=lastMatch.getPlayers();
		for(PlayerVO player: lastMatchPlayers)
		{
			String subnet=StringUtils.getSubnet(player.getIP());
			Long crashcount=subnetFreqHash.get(subnet);
			if(crashcount>1)
				sb.append("\tPlayer:"+player.getPlayerName()+"  Subnet: "+subnet+ "  Frequency:"+subnetFreqHash.get(subnet)+"\n");
		}
		GUI.logToStatus(sb.toString());
		
	}

	private void refreshSelectedTab()
	{
		int displayedTabIndex = playerListTabbedPane.getSelectedIndex();
		// if(PropertyManager.isDebug())
		System.out.println(Messages.getString("GUI.149") + playerListTabbedPane.getSelectedIndex());
		playerThreads.get(displayedTabIndex).getDataRefreshTablesAndSave(System.currentTimeMillis());
	}

	@Override
	public void windowActivated(WindowEvent e)
	{
	}

	@Override
	public void windowClosed(WindowEvent e)
	{
	}

	@Override
	public void windowClosing(WindowEvent e)
	{
		Object[] buttons =
		{ Messages.getString("GUI.150"), Messages.getString("GUI.151") };
		int n = JOptionPane.showOptionDialog(this, Messages.getString("GUI.152"), Messages.getString("GUI.153"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, /* icon *///$NON-NLS-2$
		buttons, buttons[1]);
		if (n == 0)
		{
			// exit
			PropertyManager.saveToDisk();
			if (db == null) db = new DBUtil();
			db.disconnect();
			System.out.println(Messages.getString("GUI.154"));
			System.exit(0);
		}
		else
		{
			// send to tray
			this.setVisible(false);
		}
	}

	@Override
	public void windowDeactivated(WindowEvent e)
	{
	}

	@Override
	public void windowDeiconified(WindowEvent e)
	{
		refreshSelectedTab();
	}

	@Override
	public void windowIconified(WindowEvent e)
	{
		if (trayOnMinimizeMenuItem.isSelected()) this.setVisible(false);
	}

	@Override
	public void windowOpened(WindowEvent e)
	{
	}

	static StringBuilder sb = new StringBuilder();
	static DateFormat dateFormat = new SimpleDateFormat(Messages.getString("GUI.155"));

	public synchronized static void logToStatus(String str)
	{
		sb.setLength(0);
		sb.append(dateFormat.format(new Date()));
		sb.append(str);
		System.out.println(sb.toString());
		sb.append(Messages.getString("GUI.156"));
		sb.append(statusTextArea.getText());
		statusTextArea.setText(sb.toString());
		statusTextArea.setCaretPosition(0);
	}

	public static String getSearchResultsTextAreaText()
	{
		return searchResultsTextArea.getText();
	}

	public static void setSearchResultsTextAreaText(String value)
	{
		if (value != null) GUI.searchResultsTextArea.setText(value);
	}

	public static void setSearchResultsTextAreaCaretPosition(int pos)
	{
		searchResultsTextArea.setCaretPosition(pos);
	}

	// tray single click
	@Override
	public void mouseClicked(MouseEvent e)
	{
		if (e.getButton() == 1)// left
		{
			if (this.isVisible())
			{
				this.setVisible(false);
			}
			else
			{
				this.setVisible(true);
				this.setExtendedState(NORMAL);// restore if minimized
				GUI.redraw();
				refreshSelectedTab();

			}
		}
		// System.out.println("Tray Icon - Mouse clicked!");
	}

	@Override
	public void mouseEntered(MouseEvent e)
	{
		// System.out.println("Tray Icon - Mouse entered!");
	}

	@Override
	public void mouseExited(MouseEvent e)
	{
		// System.out.println("Tray Icon - Mouse exited!");
	}

	@Override
	public void mousePressed(MouseEvent e)
	{
		// System.out.println("Tray Icon - Mouse pressed!");
	}

	@Override
	public void mouseReleased(MouseEvent e)
	{
		// System.out.println("Tray Icon - Mouse released!");
	}

	@Override
	public void stateChanged(ChangeEvent e)
	{
		// if (playerListTabbedPane != null)
		// System.out.println("You are on Tab #" +
		// playerListTabbedPane.getSelectedIndex() + " Of the player list");
		// if (searchAndLogsTabbedPane != null)
		// System.out.println("You are on Tab #" +
		// searchAndLogsTabbedPane.getSelectedIndex() + " left tabs pane");
	}

	public static void getUsernamePassword()
	{
		// show two dialogs, get username and password
		String username = (String) JOptionPane.showInputDialog(null, Messages.getString("GUI.157"), Messages.getString("GUI.158"), JOptionPane.QUESTION_MESSAGE, null, null, PropertyManager.getwebadmin_username());
		// System.out.println("Username="+username);
		String password = (String) JOptionPane.showInputDialog(null, Messages.getString("GUI.159") + username + Messages.getString("GUI.160"), Messages.getString("GUI.161"), JOptionPane.QUESTION_MESSAGE, null, null, PropertyManager.getwebadmin_password());
		// System.out.println("Password="+password);
		// update the property file

		PropertyManager.setwebadmin_username(username);
		PropertyManager.setwebadmin_password(password);
		PropertyManager.saveToDisk();
	}

	public static void kickIP(String ip)
	{
		System.out.println(Messages.getString("GUI.162") + ip);
		int url_number = GUI.playerListTabbedPane.getSelectedIndex();
		RollOverJTable table = matchSnapShotTables[url_number];
		// get the table model and find the web_id corresponding to this ip
		// address

		for (int i = 0; i < table.getModel().getRowCount(); i++)
		{
			if (((String) table.getModel().getValueAt(i, 4)).equals(ip))
			{
				// player to be kicked is at row i
				String name = (String) table.getModel().getValueAt(i, 0);
				String team = (String) table.getModel().getValueAt(i, 5);
				String webID = (String) table.getModel().getValueAt(i, 6);
				if (JOptionPane.showConfirmDialog(inst, Messages.getString("GUI.163") + name + Messages.getString("GUI.164") + ip + Messages.getString("GUI.164") + team + Messages.getString("GUI.166"), Messages.getString("GUI.167"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) //$NON-NLS-5$
				{
					System.out.println(Messages.getString("GUI.168"));
					// call HTMLStreamUtils to do the kick.pass the web id
					HTMLStreamUtils.kickID(url_number, webID);
					logToStatus(Messages.getString("GUI.169") + name + Messages.getString("GUI.164") + ip + Messages.getString("GUI.164") + team + Messages.getString("GUI.172"));
				}
				//this will keep asking till the right person is found.if many ahve the same ip.
			}
		}
	}

	public static void banIP(String ip)
	{
		System.out.println(Messages.getString("GUI.173") + ip);
		int url_number = playerListTabbedPane.getSelectedIndex();
		RollOverJTable table = matchSnapShotTables[url_number];
		// get the table model and find the web_id corresponding to this ip
		// address

		for (int i = 0; i < table.getModel().getRowCount(); i++)
		{
			if (((String) table.getModel().getValueAt(i, 4)).equals(ip))
			{
				// player to be kicked is at row i
				String name = (String) table.getModel().getValueAt(i, 0);
				String team = (String) table.getModel().getValueAt(i, 5);
				String webID = (String) table.getModel().getValueAt(i, 6);
				if (JOptionPane.showConfirmDialog(inst, Messages.getString("GUI.174") + name + Messages.getString("GUI.164") + ip + Messages.getString("GUI.164") + team + Messages.getString("GUI.177"), Messages.getString("GUI.178"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) //$NON-NLS-5$
				{
					System.out.println(Messages.getString("GUI.179"));
					// call HTMLStreamUtils to do the Ban.pass the web id
					HTMLStreamUtils.banID(url_number, webID);
					logToStatus(Messages.getString("GUI.180") + name + Messages.getString("GUI.164") + ip + Messages.getString("GUI.164") + team + Messages.getString("GUI.183"));
				}
				break;

			}
		}
	}

	public static void setLastRefreshTextField(String text)
	{
		GUI.lastRefreshTextField.setText(text);
	}

	public static boolean getDontUpdateValue()
	{
		return GUI.dontUpdateScreenMenuItem.isSelected();
	}

	public static boolean isWindowShowing()
	{
		return inst.isShowing();
	}

	public static void redraw()
	{
		inst.repaint();
	}

	public void paint(Graphics g)
	{
		super.paint(g);
		if (inst != null && inst.currentRefreshLabel != null && !inst.currentRefreshLabel.getText().equals(PropertyManager.getGrab_interval()))
		{
			inst.currentRefreshLabel.setText(PropertyManager.getGrab_interval());
		}
	}

	// public void setPlayerListControlsValue(String text)
	// {
	// //playerListControls.setText(text);
	// }

}
